package cn.com.twoke.application.game.mario.game.entity;

import cn.com.twoke.application.game.mario.core.ecs.world.GameWorld;
import cn.com.twoke.application.game.mario.core.ecs.component.RigidbodyComponent;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import cn.com.twoke.application.game.mario.game.component.TransfersComponent;
import cn.hutool.core.lang.Opt;

import java.awt.*;

public class TransferBuilder extends AbstractEntityBuilder<TransferBuilder>{


    private TransfersComponent.TransferType type;
    private String entryName;
    private int width;
    private int height;
    /**
     * 目标场景名称。当改传送门作出口时，必须要携带的参数
     */
    private String targetSceneName;

    public TransferBuilder(GameWorld world) {
        super(world);
    }

    public TransferBuilder type(TransfersComponent.TransferType type) {
        this.type = type;
        return this;
    }

    public TransferBuilder entryName(String entryName) {
        this.entryName = entryName;
        return this;
    }

    public TransferBuilder width(int width) {
        this.width = width;
        return this;
    }

    public TransferBuilder height(int height) {
        this.height = height;
        return this;
    }

    public TransferBuilder targetSceneName(String targetSceneName) {
        this.targetSceneName = targetSceneName;
        return this;
    }

    @Override
    public Entity build() {
        Entity entity = world.createEntity();
        entity.setType(EntityType.TRANSFER);
        TransfersComponent transfersComponent = new TransfersComponent();
        TransfersComponent.Transfer transfer = transfersComponent.createTransfer();
        // 传送门入口类型
        transfer.type = Opt.ofNullable( type).orElse(TransfersComponent.TransferType.ENTRY);
        // 开启传送门
        transfer.enabled = true;
        // 传送门名称，名称的作用是绑定出入口关系
        transfer.setName(Opt.ofNullable(entryName).orElse(""));
        transfer.shape = new Rectangle(startX, startY,
                width, height
        );
        transfer.setTargetSceneName(Opt.ofNullable(targetSceneName).orElse(""));

        RigidbodyComponent rigidbodyComponent = new RigidbodyComponent();
        rigidbodyComponent.bodyType = RigidbodyComponent.BodyType.STATIC;

        entity.addComponent(rigidbodyComponent);
        entity.addComponent(transfersComponent);
        // 扩展操作
        Opt.ofNullable(extend).ifPresent(extend ->  extend.accept(entity));
        return entity;
    }
}
